{
 "cells": [
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 初始化"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import datetime\n",
    "import pandas as pd\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "# macOS\n",
    "# plt.rcParams['font.sans-serif'] = ['Arial Unicode MS']\n",
    "# Windows\n",
    "plt.rcParams['font.sans-serif'] = ['Microsoft YaHei']\n",
    "# 正常显示负号\n",
    "plt.rcParams['axes.unicode_minus'] = False\n",
    "work_dir = os.getcwd()"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 公开数据处理"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 负荷原始数据预处理\n",
    "df = pd.read_excel(f'{work_dir}/../Data/load_2018.xlsx', sheet_name='负荷')\n",
    "df.head()\n",
    "load = df[df.columns[1:]].values.reshape(-1, 1).flatten()\n",
    "load\n",
    "time, season, month, quarter, temperature, humidity, wind, water = [], [], [], [], [], [], [], []\n",
    "time0 = datetime.datetime(2018, 1, 1)\n",
    "for r in range(load.shape[0]):\n",
    "    t = time0 + datetime.timedelta(r // 96)\n",
    "    time.append(str(t.date()))\n",
    "    if t.month in [3, 4, 5]:\n",
    "        season.append(1)\n",
    "    elif t.month in [6, 7, 8]:\n",
    "        season.append(2)\n",
    "    elif t.month in [9, 10, 11]:\n",
    "        season.append(3)\n",
    "    else:\n",
    "        season.append(4)\n",
    "    quarter.append(r % 96)\n",
    "    month.append(t.month)\n",
    "sheet_names = ['温度', '湿度', '风速', '降雨']\n",
    "for s in sheet_names:\n",
    "    df = pd.read_excel(f'{work_dir}/../Data/load_2018.xlsx', sheet_name=s)\n",
    "    data = df[df.columns[1:]].values.reshape(-1, 1).flatten()\n",
    "    if s == '温度':\n",
    "        temperature = data\n",
    "    elif s == '湿度':\n",
    "        humidity = data\n",
    "    elif s == '风速':\n",
    "        wind = data\n",
    "    else:\n",
    "        water = data\n",
    "load_data = pd.DataFrame({'时间': time, '时节': season, '月份': month, '时刻': quarter,\n",
    "                          '温度': temperature, '湿度': humidity, '风速': wind, '降雨': water, '实际功率': load})\n",
    "load_data.head()\n",
    "load_data.to_csv(f'{work_dir}/../Data/处理后数据/load_2018.csv', encoding='gbk')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "import os\n",
    "import datetime\n",
    "import pandas as pd\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "# macOS\n",
    "# plt.rcParams['font.sans-serif'] = ['Arial Unicode MS']\n",
    "# Windows\n",
    "plt.rcParams['font.sans-serif'] = ['Microsoft YaHei']\n",
    "# 正常显示负号\n",
    "plt.rcParams['axes.unicode_minus'] = False\n",
    "work_dir = os.getcwd()"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "## 不同季节光伏功率"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "df = pd.read_csv(f'{work_dir}/data/pv_2018.csv', encoding='gbk')\n",
    "df.head()\n",
    "# data = [[] for i in range(4)]\n",
    "# for i, row in df.iterrows():\n",
    "#     month = row['月份']\n",
    "#     if month == 3 or month == 4 or month == 5:\n",
    "#         data[0].append(row['实际功率'])\n",
    "#     elif month == 6 or month == 7 or month == 8:\n",
    "#         data[1].append(row['实际功率'])\n",
    "#     elif month == 9 or month == 10 or month == 11:\n",
    "#         data[2].append(row['实际功率'])\n",
    "#     else:\n",
    "#         data[3].append(row['实际功率'])\n",
    "# _ = plt.figure(figsize=(9,6))\n",
    "# for i in range(4):\n",
    "#     days = int(len(data[i])/96)\n",
    "#     dt = np.reshape(data[i], (days, 96))\n",
    "#     _ = plt.subplot(1, 4, i+1)\n",
    "#     for j in range(days):\n",
    "#         _ = plt.plot(range(60), dt[j][24:84])\n",
    "#         _ = plt.axis('equal')\n",
    "# _ = plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "import os\n",
    "import datetime\n",
    "import pandas as pd\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "# macOS\n",
    "# plt.rcParams['font.sans-serif'] = ['Arial Unicode MS']\n",
    "# Windows\n",
    "plt.rcParams['font.sans-serif'] = ['Microsoft YaHei']\n",
    "# 正常显示负号\n",
    "plt.rcParams['axes.unicode_minus'] = False\n",
    "work_dir = os.getcwd()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "import os\n",
    "import datetime\n",
    "import pandas as pd\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "# macOS\n",
    "# plt.rcParams['font.sans-serif'] = ['Arial Unicode MS']\n",
    "# Windows\n",
    "plt.rcParams['font.sans-serif'] = ['Microsoft YaHei']\n",
    "# 正常显示负号\n",
    "plt.rcParams['axes.unicode_minus'] = False\n",
    "work_dir = os.getcwd()"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "## 红船数据测试"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "%load_ext autoreload\n",
    "%autoreload 2\n",
    "\n",
    "import hc_forecast\n",
    "import datetime\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "hc = hc_forecast.HcForecast()\n",
    "# hc.check_local_data()\n",
    "# hc.init_local_data()\n",
    "# hc.init_local_model()\n",
    "given_time = datetime.datetime(2023, 6, 14, 15, 17)\n",
    "for i in range(1, 2):\n",
    "    print(f\"开始设备{i}的预测...\")\n",
    "    y_pre = hc.forecast_day_ahead_sometime(i, given_time)\n",
    "    if y_pre is not None:\n",
    "        _ = plt.plot(range(96), y_pre)\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sqlalchemy import create_engine\n",
    "from sqlalchemy import text\n",
    "from urllib.parse import quote_plus as urlquote\n",
    "MYSQL_HOST = \"127.0.0.1\"\n",
    "MYSQL_PORT = \"3306\"\n",
    "MYSQL_USER = \"user\"\n",
    "MYSQL_PASSWORD = urlquote(\"123456\")\n",
    "MYSQL_DB = \"jxhc\"\n",
    "ENGINE = create_engine(\n",
    "    f\"mysql+pymysql://{MYSQL_USER}:{MYSQL_PASSWORD}@{MYSQL_HOST}:{MYSQL_PORT}/{MYSQL_DB}?charset=utf8\",\n",
    "    pool_pre_ping=True,\n",
    ")\n",
    "with ENGINE.connect() as conn:\n",
    "                predict_type = 1\n",
    "                time_delta = 5\n",
    "                # 日前\n",
    "                time = datetime.datetime.now()\n",
    "                time_delta = 15\n",
    "                time_in = time + datetime.timedelta(minutes=i * time_delta)\n",
    "                time_now = datetime.datetime.now()\n",
    "                sql = text(\n",
    "                    f\"INSERT INTO x_prediction_curve (predict_type, photovoltaic_id, equipment_p, date, predict_curve_type, update_time) VALUES ({predict_type}, 1, {round(34.1111, 2)}, date_format('{time_in}', '%Y-%m-%d %H:%i:%s'), 1, date_format('{time_now}', '%Y-%m-%d %H:%i:%s'));\"\n",
    "                )\n",
    "                conn.execute(sql)\n",
    "                conn.commit()\n",
    "                conn.close()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.16"
  },
  "vscode": {
   "interpreter": {
    "hash": "e1582c2b6d08d091242bbba91b4102c1fdf89e601d9e3a7a12a6df745e337292"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
